/**
 * @Author:Geln Yang
 * @Created:2008-7-29
 * $ Revision: 1.0 $
 * $ Author: Geln Yang $
 * $ Date: 2008-7-29 $
 */
package org.geln.string;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.apache.log4j.Logger;

/**
 * @Author:Geln Yang
 * @Created:2008-7-29
 */
public class StringBufferUtil {
	private static Logger logger = Logger.getLogger(StringBufferUtil.class);

	public static StringBuffer getStringBuffer(Reader is) {
		if (is != null) {
			int c;
			try {
				StringBuffer sb = new StringBuffer();

				while ((c = is.read()) > 0) {
					sb.append((char) c);
				}
				return sb;
			} catch (IOException e) {
				error(e);
			}
		}
		return null;

	}

	public static StringBuffer getStringBuffer(InputStream is) {
		if (is != null) {
			try {
				StringBuffer sb = new StringBuffer();
				int size = 0;
				byte[] buf = new byte[1024];
				while ((size = is.read(buf)) != -1) {
					sb.append(new String(buf, 0, size));
				}
				return sb;
			} catch (IOException e) {
				error(e);
			}
		}
		return null;

	}

	private static void error(Exception e) {
		logger.error(e.getClass().getName() + ":" + e.getMessage());
	}
}
